/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package give.replay.gui;

import java.awt.Dimension;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JSlider;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;

/**
 *
 * @author koller
 */
abstract public class JSliderAnnotator implements MouseMotionListener, MouseListener {
    private JPopupMenu pop = new JPopupMenu();
    private JSlider slider;
    private JLabel lblPop = new JLabel("", SwingConstants.CENTER);
    private int previousValue = -1;

    abstract protected String getAnnotation(int sliderValue);

    public JSliderAnnotator(JSlider slider) {
        this.slider = slider;
        JPanel jp = new JPanel();
        lblPop.setPreferredSize(new Dimension(30, 12));
        jp.add(lblPop);
        pop.add(jp);
    }

    private void setPop(MouseEvent me) {
        int val = slider.getValue();

        if (val != previousValue) {
            lblPop.setText("" + getAnnotation(val));
            Point pos = new Point(me.getX()-10, 0);
            SwingUtilities.convertPointToScreen(pos, slider);
            pop.setLocation(pos);
            previousValue = val;
        }
    }

    public void mouseDragged(MouseEvent e) {
        setPop(e);
    }

    public void mouseMoved(MouseEvent e) {
    }

    public void mouseClicked(MouseEvent e) {
    }

    public void mousePressed(MouseEvent e) {
        pop.setVisible(true);
        setPop(e);
    }

    public void mouseReleased(MouseEvent e) {
        pop.setVisible(false);
    }

    public void mouseEntered(MouseEvent e) {
    }

    public void mouseExited(MouseEvent e) {
    }
}
